package a66weding.com.jiehuntong.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.base.BaseActivity;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.pager.BasePager;
import a66weding.com.jiehuntong.pager.ToolsPager_Task_Addtask;
import a66weding.com.jiehuntong.pager.Tools_DeletetaskPager;
import a66weding.com.jiehuntong.pager.Tools_UpdatewedPager;

/**
 * Created by admin on 2016/6/1.
 */
public class Tools_MaramissActivity extends BaseActivity implements View.OnClickListener {

    private View myBodyView;
    private Button btnCancle;
    private Dialog setDialog;
    private RelativeLayout rlTimesort;
    private HomeBaseFragment mFragment;
    private static Map<String, BasePager> mTaskPage;
    private ImageView ivRsort, ivHsort, addtask_iv;
    private TextView mBackTv, mTitleTv, mAddTv, tvDeltask, tvTimesort, tvUpdatewed;
    private LinearLayout mHeadBackLay;

    @Override
    protected View getContentView() {
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("Tools_MaramissActivity", this);
        myBodyView = View.inflate(Tools_MaramissActivity.this, R.layout.activity_tools_maramiss, null);
        addtask_iv = (ImageView) myBodyView.findViewById(R.id.bianjiStory_addtask_iv);
        mHeadBackLay = (LinearLayout) myBodyView.findViewById(R.id.lay_head_backlay);

        mBackTv = (TextView) myBodyView.findViewById(R.id.headleft_title_tv);
        mTitleTv = (TextView) myBodyView.findViewById(R.id.headcenter_title_tv);
        mAddTv = (TextView) myBodyView.findViewById(R.id.headright_title_tv);

        mTitleTv.setText("结婚任务");
        mAddTv.setText("设置");
        mBackTv.setText("工具");

        addtask_iv.setOnClickListener(this);
        mHeadBackLay.setOnClickListener(this);
        mAddTv.setOnClickListener(this);
        return myBodyView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bianjiStory_addtask_iv:
                CUtils.showToast("添加任务");
                apptaskPager("添加任务");
                break;
            case R.id.lay_head_backlay:
                finish();
                break;
            case R.id.headright_title_tv:
                //我的设置
                showMyPop();
                break;
            case R.id.setting_cancel_btn:
                //取消
                setDialog.dismiss();
                break;
            case R.id.setting_deltask_tv:
                //删除任务
                deleteTaskPager("删除任务");
                setDialog.dismiss();
                break;
            case R.id.setting_updatewed_tv:
                //修改婚期
                updateWedPager("修改婚期");
                setDialog.dismiss();
                break;
            case R.id.setting_timesort_rl:
                //时间排序
                break;
        }
    }

    /**
     * 弹出popupwindow修改婚期、删除任务、时间排序
     */
    private void showMyPop() {
        setDialog = new Dialog(this, R.style.dialog);
        setDialog.setContentView(R.layout.tools_setting_dialog);
        btnCancle = (Button) setDialog.findViewById(R.id.setting_cancel_btn);
        rlTimesort = (RelativeLayout) setDialog.findViewById(R.id.setting_timesort_rl);
        tvDeltask = (TextView) setDialog.findViewById(R.id.setting_deltask_tv);
        tvUpdatewed = (TextView) setDialog.findViewById(R.id.setting_updatewed_tv);
        ivRsort = (ImageView) setDialog.findViewById(R.id.setting_rsort_iv);


        tvDeltask.setOnClickListener(this);
        tvUpdatewed.setOnClickListener(this);
        btnCancle.setOnClickListener(this);
        rlTimesort.setOnClickListener(this);

        Window window = setDialog.getWindow();
        // 可以在此设置显示动画
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = getWindowManager().getDefaultDisplay().getHeight();
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 设置显示位置
        setDialog.onWindowAttributesChanged(wl);
        setDialog.show();
    }

    /**
     * 删除任务
     *
     * @param pagerName
     */
    private void deleteTaskPager(String pagerName) {
        mTaskPage = new HashMap<String, BasePager>();
        mTaskPage.put(pagerName, new Tools_DeletetaskPager(this));
        setFragment(pagerName);
    }

    /**
     * 修改日期
     *
     * @param pagerName
     */
    private void updateWedPager(String pagerName) {
        mTaskPage = new HashMap<String, BasePager>();
        mTaskPage.put(pagerName, new Tools_UpdatewedPager(this));
        setFragment(pagerName);
    }

    /**
     * 添加任务
     *
     * @param pagerName
     */
    private void apptaskPager(String pagerName) {
        mTaskPage = new HashMap<String, BasePager>();
        mTaskPage.put(pagerName, new ToolsPager_Task_Addtask(this));
        setFragment(pagerName);

    }

    private void setFragment(String pagerName) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        mFragment = new HomeBaseFragment();
        Bundle mBundle = new Bundle();
        mBundle.putString("pagerName", pagerName);

        mFragment.setArguments(mBundle);

        transaction.replace(R.id.flay_baseactivity_content, mFragment);
        transaction.commit();
    }

    public static class HomeBaseFragment extends Fragment {

        public HomeBaseFragment() {
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            String pagerName = (String) getArguments().get("pagerName");
            BasePager pager = getBasePager(pagerName);
            if (null != pager)
                return pager.mRootView;
            return null;
        }


    }

    /**
     * 根据索引位置获取指定的 BasePager 页面
     */
    public static BasePager getBasePager(String pagerName) {
        BasePager pager = mTaskPage.get(pagerName);
        if (null != pager && !pager.mInit) {
            pager.mInit = true;
            pager.initData();
        }
        return pager;
    }
}
